﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;

using TImport = starLiGHT.Content.SpriteListContent;
#endregion

#region LICENSE

/*
    This file is part of starLiGHT.Content.

    starLiGHT.Content is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    starLiGHT.Content is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with starLiGHT.Content.  If not, see <http://www.gnu.org/licenses/>.

    
    ADDITIONAL (commercial) LICENSES for starLiGHT.Content are available on request.
*/

#endregion

#region Version Stuff
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 1514                  $:  Revision of last commit *
// * $Author:: glatzemann         $:  Author of last commit   *
// * $Date:: 2010-10-12 10:12:09 #$:  Date of last commit     *
// ************************************************************
#endregion

namespace starLiGHT.Content
{
    [ContentImporter(".SpriteSheet", DisplayName = "SpriteSheet Importer - starLiGHT.Engine", DefaultProcessor = "SpriteSheet Processor - starLiGHT.Engine")]
    public class SpriteSheetImporter : ContentImporter<TImport>
    {
        public override TImport Import(string filename, ContentImporterContext context)
        {
            String[] lines = File.ReadAllLines(filename);

            SpriteListContent ssc = new SpriteListContent();

            int lineNumber = 0;
            foreach (string line in lines)
            {
                string l = line;
                String[] parts = null;
                if (!l.Trim().StartsWith("#"))
                {
                    if (l.Contains("#")) l = l.Split('#')[0];
                    parts = l.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);

                    if (parts != null && parts.Length == 2)
                    {
                        for (int i = 0; i < parts.Length; i++)
                            parts[i] = parts[i].Trim();

                        if (parts[0].Equals("File", StringComparison.InvariantCultureIgnoreCase))
                        {
                            context.Logger.LogImportantMessage("adding file: '{0}'", parts[1].Trim());
                            ssc.AddFile(parts[1].Trim());
                        }
                        else if (parts[0].Equals("Directory", StringComparison.InvariantCultureIgnoreCase))
                        {
                            context.Logger.LogImportantMessage("adding files of directory '{0}'", parts[1]);
                            String[] files = Directory.GetFiles(parts[1]);
                            foreach (string file in files)
                            {
                                context.Logger.LogImportantMessage("adding file: '{0}'", file);
                                ssc.AddFile(file);
                            }
                        }
                        else
                        {
                            throw new Exception(string.Format("Invalid argument line ({0}) - {1}", lineNumber, parts[0]));
                        }
                    }
                    else
                    {
                        throw new Exception(string.Format("Invalid line: ({0}) - {1}", lineNumber, line));
                    }
                }

                lineNumber++;
            }

            return ssc;
        }
    }
}
